import cv2
import time
import os
import numpy as np
import scipy.interpolate as spi
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline

def draw_line_do(event, x, y, flags, param):
    global previous, point_num, x_0, y_0
    draw_point=[]

    # point_path = "/home/ltt/point_track/draw_point/"+"93_0"+".txt"#师姐的
    # if not os.path.exists(point_path):
    #     os.makedirs(point_path)

    if event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:#按住左键
        ix, iy = x, y
        currennt = time.time()
        duration = currennt - previous
        if duration > 0.1:
            point_num = 0
        if point_num != 0:
            cv2.line(img, (x_0, y_0), (ix, iy), color=(0, 255, 0))
        else:
            cv2.circle(img, (x_0, y_0), 3, color=(0, 255, 0))
        previous = currennt
        # with open("./draw_point/825hua.txt", 'a') as fd:
        with open( "/home/ltt/point_track/changzheng/0207_1.txt", 'a') as fd:
            fd.write(str(ix) + ' ' + str(iy) + '\n')
        # print("ix是",ix)
        draw_point.append((ix,iy))
        x_0, y_0 = ix, iy
        point_num += 1
    cv2.imwrite('./draw_point' + str(1) + '.png', img)


if __name__ == "__main__":
    x_0, y_0 = 0, 0
    previous = time.time()
    point_num = 0
    img = cv2.imread("/home/ltt/point_track/changzheng/2021-06-03_171616_VID002/00040.png", flags=1)
    # img = cv2.imread("/home/ltt/point_track/instrument/rectified_right/00002.jpg", flags=1)
    # img = cv2.imread("/home/ltt/point_track/miccai/micc/00001.png", flags=1)#师姐的数据集
    # # img = cv2.imread( "/big_data/zjy/Hamlyn/rectified01/image01/0000000000.jpg", flags=1)
    # # img= cv2.imread("/home/ltt/point_track/upsamlpe_img/huaxi_2/00000.jpg", flags=1)
    #
    cv2.namedWindow('image', cv2.WINDOW_NORMAL)
    cv2.setMouseCallback("image", draw_line_do)
    i=1
    while (1):
        cv2.imshow('image', img)
        if cv2.waitKey(1) == ord('q'):
            cv2.destroyWindow('image')
            break

